package cn.com.oyat.idea.controller.push;

import cn.com.oyat.idea.model.DlnaDevice;
import cn.com.oyat.idea.service.DlnaService;
import cn.com.oyat.idea.service.VideoPushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/dlna")
public class DlnaController {

    @Autowired
    private DlnaService dlnaService;

    @Autowired
    private VideoPushService videoPushService;

    /**
     * 发现局域网内的DLNA设备
     */
    @GetMapping("/discover")
    public List<DlnaDevice> discoverDevices() {
        try {
            return dlnaService.discoverDevices();
        } catch (IOException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    /**
     * 向指定设备推送视频
     *
     * @param deviceLocation 设备的描述文档URL（从发现接口获取）
     * @param videoUrl       要推送的视频URL
     */
    @GetMapping("/push")
    public ResponseEntity<String> pushVideo(@RequestParam String deviceLocation,
                                            @RequestParam String videoUrl) {
        try {
            DlnaDevice device = new DlnaDevice();
            device.setLocation(deviceLocation);

            boolean success = videoPushService.pushVideoToDevice(device, videoUrl);
            if (success) {
                return ResponseEntity.ok("Video pushed successfully to device");
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                        .body("Failed to push video to device");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("Error: " + e.getMessage());
        }
    }
}